package com.cy.ppj.web.admin.finance;

import com.cy.ppj.model.enums.MessageType;
import com.cy.ppj.service.message.MessageService;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cy.ppj.comm.utils.CommonUtils;
import com.cy.ppj.comm.utils.ExportExcelUtils;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.query.WithdrawAdminQuery;
import com.cy.ppj.model.vo.WithdrawAdminVO;
import com.cy.ppj.service.withdraw.WithdrawService;
import com.cy.ppj.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 后台-提现管理
 * @author: tanyilin
 * @create: 2018年7月31日 下午4:33:37
 */
@Slf4j
@RestController
@RequestMapping("/admin/withdraw")
@Api(tags = "后台-提现管理")
public class AdminWithdrawController extends BaseController{

	@Autowired
	private WithdrawService withdrawService;
	@Autowired
	private MessageService messageService;
	
	@ApiOperation("提现记录列表")
	@GetMapping("/list")
	public PageBean<WithdrawAdminVO> withdrawList(WithdrawAdminQuery query) {
		return withdrawService.queryWithdrawListAdmin(query);
	}
	
	@ApiOperation("成功提现统计")
	@GetMapping("/statistics")
	public Result statistics() {
		return withdrawService.selectWithdrawStatistics();
	}
	
	@ApiOperation("提现拒绝")
	@PostMapping("/return")
	public Result withdrawRetuan(@ApiParam(value = "提现记录ID", example = "0", required = true) @RequestParam Long withdrawId) {
		withdrawService.withdrawReturn(withdrawId);
		return ok();
	}
	
	@ApiOperation("提现成功")
	@PostMapping("/success")
	public Result withdrawSuccess(@ApiParam(value = "提现记录ID", example = "0", required = true) @RequestParam Long withdrawId) {
		withdrawService.withdrawSuccess(withdrawId);
		//发送提现成功消息
		messageService.sendMessage(String.valueOf(withdrawId), MessageType.WITHDRAW_SUCCESS);
		return ok();
	}
	
	@ApiOperation("导出Excel")
	@GetMapping("/list/export/excel")
	public void listExportExcel(WithdrawAdminQuery query, HttpServletRequest req, HttpServletResponse resp) {
		query.setPage(1);
		query.setLimit(5000);
		List<WithdrawAdminVO> data = this.withdrawList(query).getData();
		String title = "提现记录列表";
		String[] headers = {"申请时间", "提现金额", "客户姓名", "城市", "提现银行", "银行卡号", "预留手机号", "提现状态"};
		String[] fieldNames = {"createTime", "amount", "realName", "city", "bankName", "bankCard", "bankMobile", "statusText"};
		ExportExcelUtils<WithdrawAdminVO> ex = new ExportExcelUtils<>();
		try(OutputStream out = resp.getOutputStream()) {
			req.setCharacterEncoding("UTF-8");
			resp.setCharacterEncoding("UTF-8");
			resp.setContentType("application/x-download");
			String fileName = CommonUtils.getCurrentTimeAndRandomNumber().concat(".xls");
			fileName = new String(fileName.getBytes("gb2312"), "ISO8859-1");
			resp.setHeader("Content-Disposition", "attachment;filename=" + fileName);
			ex.exportExcel(title, headers, fieldNames, data, out);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
